package linter.linterjava.controller.model;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import linter.linterjava.POJO.DO.Model;
import linter.linterjava.POJO.DTO.ModelDTO;
import linter.linterjava.POJO.DTO.ModelUserDataDTO;
import linter.linterjava.POJO.DTO.UserDTO;
import linter.linterjava.POJO.UtilDTO.FileDTO;
import linter.linterjava.POJO.UtilDTO.PageInfoDTO;
import linter.linterjava.custom.ResultVO;
import linter.linterjava.service.ModelService;
import linter.linterjava.service.UserQueryService;
import linter.linterjava.service.UserService;
import linter.linterjava.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static linter.linterjava.custom.CustomResultType.USER_NOT_EXIST;

/**
 * @author 北煜
 */
@Slf4j
@Api(tags = "模型基础模块")
@RequestMapping("model")
@RestController
public class BaseModelController {

    @Resource
    UserService userService;
    @Resource
    ModelService modelService;
    private final UserQueryService userQueryService;
    @Autowired
    public BaseModelController(UserQueryService userQueryService) { this.userQueryService = userQueryService; }
    @GetMapping("/show/getUserByUsername/{username}")
    @ApiOperation("根据用户名查找用户")
    public ResultVO<UserDTO> getUserByUsername(@PathVariable String username) { return userQueryService.getUserByUsername(username); }

    // TODO：前端返回：处理好的文件，记录处理方法的对象？
    // TODO：1. 处理好的文件在数据库里面是url的链接。 2. 得到用户选择好的特征列表、数据和模型的参数进行拟合给出初步数据结果（创建一个保存模型特殊参数的字段表）
    // TODO：3. 把预测结果合并测试集后返回前端（csv文件之类）

    /**
     * 适用训练集/测试集上传
     *
     * @param modelUserDataDTO 用户模型文件类 -- 文件，选择的特征列表
     */
    @PostMapping("uploadDataForModel")
    @ApiOperation("数据集上传 -- 选择的特征列表")
    public ResultVO<Long> uploadDataForModel(@RequestBody ModelUserDataDTO modelUserDataDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(modelService.uploadData(modelUserDataDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("uploadByUserSelDataPreprocess")
    @ApiOperation("上传选择好的预处理方法")
    public ResultVO<String> uploadByUserSelDataPreprocess(@RequestBody ModelUserDataDTO modelUserDataDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null) { return ResultVO.success(modelService.uploadByUserSelDataPreprocess(modelUserDataDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("selectAllModel")
    @ApiOperation("查询所有模型")
    public ResultVO<PageInfo<Model>> selectAllModel(@RequestBody PageInfoDTO pageInfoDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(modelService.selectAllModel(pageInfoDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("queryModel")
    @ApiOperation("搜索模型")
    public ResultVO<List<Model>> queryModel(@RequestBody ModelDTO modelDTO) {
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(modelService.searchModel(modelDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }




}
